/************************************************************************/
// Copyright (C) 2016, han_gangbiao. All rights reserved.
// Use of this source code is governed by a GPL-v2 license that can be found in the License file.
// 
// [Time]:      2016-1-4 21:53:17
// [Author]:    han_gangbiao [http://my.oschina.net/jackhen]
// [Info]:      
/************************************************************************/

#ifndef INCLUDE_NET_BINARY_SESSION_H_
#define INCLUDE_NET_BINARY_SESSION_H_

#include "net_binary_pdu.h"

class IBinaryDecoderListener;
class NetBinaryDecoder
{
public:
	NetBinaryDecoder( IBinaryDecoderListener* listener );
	~NetBinaryDecoder();

public:
	int Parse( int fd, const char* data, int len );
protected:
	int GetContentLength( smart::ref_ptr<NetBinaryPdu> pdu ) const;
protected:
	smart::ref_ptr<NetBinaryPdu> m_tmpPdu;

	char* m_buffer;
	int m_dataLen;

	char* m_body;
	int m_bodyLen;

private:
	IBinaryDecoderListener* m_listener;
};

typedef enum BinaryDecoderError
{
	BINARY_DECODER_ERROR_UNKNOWN = 0,
	BINARY_DECODER_ERROR_PARSE_FAILED,
	BINARY_DECODER_ERROR_BUFFER_FULL,
	BINARY_DECODER_ERROR_PACKET_LACK,
}BinaryDecoderError_t;

class IBinaryDecoderListener
{
public:
	virtual ~IBinaryDecoderListener() { }
public:
	virtual int onPDU( int fd, smart::ref_ptr<NetBinaryPdu> pdu ) = 0;
	virtual int onError( int fd, BinaryDecoderError_t err ) = 0;
};

#endif //~INCLUDE_NET_BINARY_SESSION_H_

